package com.btpj.module_main.ui.home

import androidx.paging.PageKeyedDataSource
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.btpj.jetpack.base.BaseListFragment
import com.btpj.jetpack.datasource.MutablePageKeyedDataSource
import com.btpj.jetpack.entity.DataBean
import com.btpj.module_main.adapter.HomeListAdapter
import com.scwang.smartrefresh.layout.api.RefreshLayout

class HomeListFragment : BaseListFragment<DataBean,HomeListViewModel>() {

    companion object {
        fun newInstance() = HomeListFragment()
    }

    override fun onCreateViewAfter() {
        super.onCreateViewAfter()
        hiddenActionBar()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.mDataSource!!.invalidate()
        finishRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        //当加载更多的时候返回了空数据 需要手动接管数据加载否则paging框架将不会继续加载数据
        val currentList = mAdapter.currentList
        if (currentList == null || currentList.size <= 0) {
            return
        }

        val key = currentList.size / 20
        mViewModel.getHomeList(
            key,
            key + 1,
            object : PageKeyedDataSource.LoadCallback<Int, DataBean>() {
                override fun onResult(data: MutableList<DataBean>, adjacentPageKey: Int?) {
                    //把data转成pageList
                    val dataSource = MutablePageKeyedDataSource<DataBean>()
                    dataSource.data.addAll(currentList)
                    dataSource.data.addAll(data)
                    val pageList =
                        dataSource.buildNewPageList(mAdapter.currentList!!.config)
                    mAdapter.submitList(pageList)
                }
            })

        finishRefresh()
    }

    @Suppress("UNCHECKED_CAST")
    override fun generateAdapter(): PagedListAdapter<DataBean, RecyclerView.ViewHolder> {
        return HomeListAdapter(requireContext()) as PagedListAdapter<DataBean, RecyclerView.ViewHolder>
    }

}